import http from './http.js'
import storage from '../util/storage.js'
const baseUrl = 'http://123.57.7.108:2048/emall';
export default {
	api_login: function(url, data) {
		return new Promise((reslove, reject) => {
			uni.request({
				method: 'POST',
				url: baseUrl + url,
				data: data,
				success: (res) => {
					if (res.data.code == 200) {
						reslove(res);
					} else {
						reject('登录失败！');
					}
				},
				fail: (error) => {
					reject(error);
				}
			})
		})
	},
	api_getCustome: function(url) {
		const token = uni.getStorageSync('token');
		const tokenHead = uni.getStorageSync('tokenHead');
		if (token && tokenHead) {
			return new Promise((reslove, reject) => {
				uni.request({
					header: {
						'Authorization': tokenHead + ' ' + token
					},
					method: 'Get',
					url: baseUrl + url,
					success: res => {
						if (res.data.code == 200) {
							reslove(res)
						} else {
							reject('用户数据获取失败！')
						}
					},
					fail: error => {
						reject(error)
					}
				})
			})
		} else {
			return new Promise(() => {})
		}
	},
	api_skus: function(object) { //商品请求信息
		const token = uni.getStorageSync('token');
		const tokenHead = uni.getStorageSync('tokenHead');
		if (token && tokenHead) {
			return new Promise((reslove, reject) => {
				uni.request({
					header: {
						'Authorization': tokenHead + ' ' + token
					},
					method: 'Get',
					url: baseUrl +'/skus',
					data: {
						pageNum: object.pageNum,
						pageSize: object.pageSize,
						query: object.query
					},
					success: res => {
						if (res.data.code == 200) {
							reslove(res)
						} else {
							reject('用户数据获取失败！')
						}
					},
					fail: error => {
						reject(error)
					}
				})
			})
		} else {
			return new Promise(() => {})
		}
	},
	api_spudetails:function(object){
		const { id } = object;
		const token = uni.getStorageSync('token');
		const tokenHead = uni.getStorageSync('tokenHead');
		if (token && tokenHead) {
			return new Promise((reslove, reject) => {
				uni.request({
					header: {
						'Authorization': tokenHead + ' ' + token
					},
					method: 'Get',
					url: baseUrl +'/spudetails/'+object.id,
					success: res => {
						if (res.data.code == 200) {
							reslove(res)
						} else {
							reject('用户数据获取失败！')
						}
					},
					fail: error => {
						reject(error)
					}
				})
			})
		} else {
			return new Promise(() => {})
		}
	},
	api_categories:function(url){
		const token = uni.getStorageSync('token');
		const tokenHead = uni.getStorageSync('tokenHead');
		if (token && tokenHead) {
			return new Promise((reslove, reject) => {
				uni.request({
					header: {
						'Authorization': tokenHead + ' ' + token
					},
					method: 'Get',
					url: baseUrl + url,
					success: res => {
						if (res.data.code == 200) {
							reslove(res)
						} else {
							reject('用户数据获取失败！')
						}
					},
					fail: error => {
						reject(error)
					}
				})
			})
		} else {
			return new Promise(() => {})
		}
	},
	api_addToCar:function(params){
		const {  cartId, count, skuId } = params; 
		const token = uni.getStorageSync('token');
		const tokenHead = uni.getStorageSync('tokenHead');
		if (token && tokenHead) {
			return new Promise((reslove, reject) => {
				uni.request({
					header: {
						'Authorization': tokenHead + ' ' + token
					},
					data:{
						cartId, count, skuId
					},
					method: 'PUT',
					url: baseUrl + '/wx/cart',
					data:{
						cartId, count, skuId
					},
					success: res => {
						if (res.data.code == 200) {
							reslove(res)
						} else {
							reject('用户数据获取失败！')
						}
					},
					fail: error => {
						reject(error)
					}
				})
			})
		} else {
			return new Promise(() => {})
		}
	},
	api_getCart:function(){
		const id = uni.getStorageSync('user').id;
		const token = uni.getStorageSync('token');
		const tokenHead = uni.getStorageSync('tokenHead');
		if (token && tokenHead) {
			return new Promise((reslove, reject) => {
				uni.request({
					header: {
						'Authorization': tokenHead + ' ' + token
					},
					method: 'GET',
					url: baseUrl + '/cart/'+id,
					success: res => {
						if (res.data.code == 200) {
							reslove(res)
						} else {
							reject('用户数据获取失败！')
						}
					},
					fail: error => {
						reject(error)
					}
				})
			})
		} else {
			return new Promise(() => {})
		}
	},
	api_updateCart:function(object){
		const token = uni.getStorageSync('token');
		const tokenHead = uni.getStorageSync('tokenHead');
		if (token && tokenHead) {
			return new Promise((reslove, reject) => {
				uni.request({
					header: {
						'Authorization': tokenHead + ' ' + token
					},
					method: 'PUT',
					url: baseUrl + '/wx/cart/',
					data:object,
					success: res => {
						if (res.data.code == 200) {
							reslove(res)
						} else {
							reject('用户数据获取失败！')
						}
					},
					fail: error => {
						reject(error)
					}
				})
			})
		} else {
			return new Promise(() => {})
		}
	},
	api_skusPage: function(object) { //商品请求信息
		const { brandId, cateId, pageNum, pageSize, query } = object;
		const token = uni.getStorageSync('token');
		const tokenHead = uni.getStorageSync('tokenHead');
		if (token && tokenHead) {
			return new Promise((reslove, reject) => {
				uni.request({
					header: {
						'Authorization': tokenHead + ' ' + token
					},
					method: 'Get',
					url: baseUrl +'/sku-page',
					data: {
						cateId, pageNum, pageSize, query
					},
					success: res => {
						if (res.data.code == 200) {
							reslove(res)
						} else {
							reject('用户数据获取失败！')
						}
					},
					fail: error => {
						reject(error)
					}
				})
			})
		} else {
			return new Promise(() => {})
		}
	},
	api_customerAddress:function(object){
		const token = uni.getStorageSync('token');
		const tokenHead = uni.getStorageSync('tokenHead');
		const { id } = object;
		if (token && tokenHead) {
			return new Promise((reslove, reject) => {
				uni.request({
					header: {
						'Authorization': tokenHead + ' ' + token
					},
					method: 'GET',
					url: baseUrl + '/customer/addrs/'+id ,
					success: res => {
						if(res.data.code==404){
							reject('还没有添加地址')
						}else if(res.data.code ==200){
							reslove(res);
						}
					},
					fail: error => {
						reject(error)
					}
				})
			})
		} else {
			return new Promise(() => {})
		}
	},
	api_addAddress:function(object){
		const token = uni.getStorageSync('token');
		const tokenHead = uni.getStorageSync('tokenHead');
		if (token && tokenHead) {
			return new Promise((reslove, reject) => {
				uni.request({
					header: {
						'Authorization': tokenHead + ' ' + token
					},
					method: 'POST',
					url: baseUrl + '/customer/addrs/' ,
					data:object,
					success: res => {
						if(res.data.code==404){
							reject('添加失败！')
						}else if(res.data.code ==200|| res.data.code==201){
							reslove(res);
						}
					},
					fail: error => {
						reject(error)
					}
				})
			})
		} else {
			return new Promise(() => {})
		}
	},
	api_updateDefaultAddress:function(object){
		const { addrId, customerId } = object;
		const token = uni.getStorageSync('token');
		const tokenHead = uni.getStorageSync('tokenHead');
		if (token && tokenHead) {
			return new Promise((reslove, reject) => {
				uni.request({
					header: {
						'Authorization': tokenHead + ' ' + token
					},
					method: 'PUT',
					url: baseUrl + '/wx/customer/addrs/',
					data:{
						addrId,customerId
					},
					success: res => {
						if(res.data.code==200){
							reslove(res);
						}else{
							reject('默认地址设置失败！')
						}
					},
					fail: error => {
						reject(error)
					}
				})
			})
		} else {
			return new Promise(() => {})
		}
	},
	api_getAllOrder:function(object){
		const { customerId } = object;
		const token = uni.getStorageSync('token');
		const tokenHead = uni.getStorageSync('tokenHead');
		if (token && tokenHead) {
			return new Promise((reslove, reject) => {
				uni.request({
					header: {
						'Authorization': tokenHead + ' ' + token
					},
					method: 'GET',
					url: baseUrl + '/orders/'+ customerId,
					success: res => {
						if(res.data.code==200){
							reslove(res);
						}else{
							reject('默认地址设置失败！')
						}
					},
					fail: error => {
						reject(error)
					}
				})
			})
		} else {
			return new Promise(() => {})
		}
	},
	api_getOneOrder:function(object){
		const { id } = object;
		const token = uni.getStorageSync('token');
		const tokenHead = uni.getStorageSync('tokenHead');
		if (token && tokenHead) {
			return new Promise((reslove, reject) => {
				uni.request({
					header: {
						'Authorization': tokenHead + ' ' + token
					},
					method: 'GET',
					url: baseUrl + '/orders/one/'+ id,
					success: res => {
						if(res.data.code==200){
							reslove(res);
						}else{
							reject('默认地址设置失败！')
						}
					},
					fail: error => {
						reject(error)
					}
				})
			})
		} else {
			return new Promise(() => {})
		}
	},
	api_createOrder:function(object){
		const token = uni.getStorageSync('token');
		const tokenHead = uni.getStorageSync('tokenHead');
		if (token && tokenHead) {
			return new Promise((reslove, reject) => {
				uni.request({
					header: {
						'Authorization': tokenHead + ' ' + token
					},
					method: 'POST',
					url: baseUrl + '/orders/',
					data:object,
					success: res => {
						reslove(res)
					},
					fail: error => {
						reject(error)
					}
				})
			})
		} else {
			return new Promise(() => {})
		}
	}
}
